package com.tim.app.ui.adapter.someone_homepage;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.library.util.TimeUtil;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.bumptech.glide.Glide;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.server.entry.Story;
import com.tim.app.util.BitmapLoader;

import java.util.List;

import jp.wasabeef.glide.transformations.BlurTransformation;


/**
 * 故事列表的适配器
 */
public class HomepageStoryAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Story> {

    private Context mContext;

    public HomepageStoryAdapter(Context mContext, List<Story> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new InfoViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_homepage_story, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, final int position, final Story data) {
        if (data == null) {
            return;
        }

        if (null == data)
            return;
        final InfoViewHolder holder = (InfoViewHolder) mHolder;

        if (!TextUtils.isEmpty(data.getTitle())) {
            holder.tvStoryTitle.setText(data.getTitle());
        }
        if (!TextUtils.isEmpty(data.getContent())) {
            holder.tvStoryContent.setText(data.getContent());
        }
        holder.tvStoryPriseNumber.setText(mContext.getString(R.string.homepageStoryPriseNumber, String.valueOf(data.getAgreeCount())));
        holder.tvStoryCommentNumber.setText(mContext.getString(R.string.homepageStoryCommentNumber, String.valueOf(data.getCommentCount())));
        holder.rlStory.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });
        holder.tvPublishTime.setText(TimeUtil.formatCircleTime(data.getCreateTime()));

        if (!TextUtils.isEmpty(data.getImage())) {
            BitmapLoader.ins().loadImage(data.getImage(),  holder.rivStory);
            Glide.with(mContext)
                    .load(data.getImage())
                    .bitmapTransform(new BlurTransformation(mContext, AppKey.RADIUS))//高斯模糊
                    .into(holder.rivStoryBg);
        }
    }


    public class InfoViewHolder extends BaseRecyclerViewHolder {

        RelativeLayout rlStory;
        RoundedImageView rivStoryBg;
        RoundedImageView rivStory;
        TextView tvStoryTitle;
        TextView tvStoryContent;
        TextView tvStoryPriseNumber;
        TextView tvStoryCommentNumber;
        TextView tvPublishTime;

        public InfoViewHolder(View itemView) {
            super(itemView);
            rlStory = (RelativeLayout) itemView.findViewById(R.id.rlStory);
            rivStoryBg = (RoundedImageView) itemView.findViewById(R.id.rivStoryBg);
            rivStory = (RoundedImageView) itemView.findViewById(R.id.rivStory);
            tvStoryTitle = (TextView) itemView.findViewById(R.id.tvStoryTitle);
            tvStoryContent = (TextView) itemView.findViewById(R.id.tvStoryContent);
            tvStoryPriseNumber = (TextView) itemView.findViewById(R.id.tvStoryPriseNumber);
            tvStoryCommentNumber = (TextView) itemView.findViewById(R.id.tvStoryCommentNumber);
            tvPublishTime = (TextView) itemView.findViewById(R.id.tvPublishTime);
        }

    }

}
